<template>
  <div class="login-wrap">
    <div class="login-left">
        <el-form label-position="left" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px"
          class="login-ruleForm">
          <img src="../assets/img/logo.png" alt="" >
          <h3 class="title">Welcome back!</h3>
          <el-form-item prop="user">
            <span>USERNAME</span>
            <el-input type="text" v-model="ruleForm.user" auto-complete="off" placeholder="账号"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <span>PASSWORD</span>
            <el-input type="password" v-model="ruleForm.password" auto-complete="off" placeholder="密码"></el-input>
          </el-form-item>
          <el-form-item style="width:100%;">
            <el-button type="primary" @click="submitForm('ruleForm')" :loading="logining">Sign in</el-button>
          </el-form-item>
          <h4>Don’t have an account? Sign up</h4>
        </el-form>
        
    </div>
    <div class="login-right"></div>
    
  </div>
</template>
<script type="text/ecmascript-6">
import { login } from '../api/userMG'
import { setCookie, getCookie, delCookie } from '../utils/util'
// import md5 from 'js-md5'
export default {
  name: 'login',
  data() {
    return {
      codeVisible: false,
      code: '',
      //定义loading默认为false
      logining: false,
      ruleForm: {
        //username和password默认为空
        user: '',
        password: '',
      },
      userInfo:null,
      //rules前端验证
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
    // 获取存在本地的用户名密码
    this.getuserpwd()
  },
  // 里面的函数只有调用才会执行
  methods: {
    // 获取用户名密码
    getuserpwd() {
      if (getCookie('user') != '' && getCookie('pwd') != '') {
        this.ruleForm.user = getCookie('user')
        this.ruleForm.password = getCookie('pwd')
      }
    },
    //获取info列表
    submitForm(formName) {
      // this.$router.push({ path: '/home' })
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.logining = true
          // 测试通道，不为空直接登录
          // setTimeout(() => {
          //   this.logining = false
          //   this.$store.commit('login', 'true')
          //   this.$router.push({ path: '/minting' })
          // }, 1000)

          // 注释
          login(
            {
              "account": this.ruleForm.user,
              "pwd": this.ruleForm.password
            }
          ).then(res => {
            console.log(res)
            if (res && res.code == 0) {

              if (this.rememberpwd) {
                //保存帐号到cookie，有效期7天
                setCookie('user', this.ruleForm.user, 7)
                //保存密码到cookie，有效期7天
                setCookie('pwd', this.ruleForm.password, 7)
              } else {
                delCookie('user')
                delCookie('pwd')
              }
              this.userInfo = res.data
              
              //如果请求成功就让他2秒跳转路由
              setTimeout(() => {
                this.logining = false
                // 缓存token
                localStorage.setItem('logintoken', this.userInfo.token)
                // 缓存用户个人信息
                this.$store.commit('login', 'true')
                this.$router.push({ path: '/minting' })
              }, 1000)

            } else {
              this.$message.error(res.message)
              this.logining = false
              return false
            }
          })
        } else {
          this.$message.error('请输入用户名密码！')
          this.logining = false
          return false
        }
      })
    },
  }
}
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  display: flex;
}

.login-left{
  width: 800px;
  background: #fff;
  display: flex;
  justify-content: center;
}

.login-right{
  background:url("../assets/img/bg1.png") left center / cover no-repeat ;
  width: calc(100% - 800px);
}

.login-ruleForm{
  padding:0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 320px;
}
.login-ruleForm img{width: 54px;}
.title {
  margin: 30px auto 80px;
  text-align: center;
  color: #1C1E22;
  font-size: 42px;
}
.login-ruleForm span{
  color: #919191;
}
.el-form-item{
  width: 100%;
}
.el-form-item button{
  width: 100%;
  background: #000;
  color: #fff;
  border-radius: 50px;
  border: 0;
  line-height: 40px;
  margin: 80px 0;
  font-size: 20px;
}
.login-ruleForm  /deep/ .el-input__inner{
  border: 0;
  border-radius: 0;
  border-bottom: 1px solid #000 !important;
}
.login-ruleForm h4{color: #919191;font-weight: normal;}

</style>